package com.shop.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.shop.pojo.Advice;
import com.shop.pojo.Orderlist;
import com.shop.pojo.Orders;
import com.shop.pojo.Users;

public class UserAction extends SysAction {
	
	private Advice advice;
	private int log;
	private String param;
	private Orders order;

	public Advice getAdvice() {
		return advice;
	}

	public void setAdvice(Advice advice) {
		this.advice = advice;
	}

	public Orders getOrder() {
	return order;
}

public void setOrder(Orders order) {
	this.order = order;
}

	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public int getLog() {
		return log;
	}

	public void setLog(int log) {
		this.log = log;
	}

	public Users user;

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public String logoff() {
		Map map = ActionContext.getContext().getSession();
		if (map.get("user") != null) {
			map.remove("user");

		}
		return "index";
	}

	public String register() {
		Map map = ActionContext.getContext().getSession();
		Users user = (Users) map.get("tourist");
		user.setUstate(0);
		if (getUserService().register(user)) {

			return "registered";
		}
		return "unregistered";
	}

	public void checked() {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = null;

		try {
			out = response.getWriter();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (getUserService().checked(param)) {
			out.println("{\"info\":\"该用户名可用\",\"status\":\"y\"}");

		} else {
			out.println("{\"info\":\"该用户名已被注册\",\"status\":\"n\"}");
		}
	}

	public String login() {
		ActionContext act = ActionContext.getContext();//

		@SuppressWarnings("unchecked")
		HashSet<HttpSession> uol = (HashSet<HttpSession>) act.getApplication()
				.get("uol");
		if (uol == null) {
			uol = new HashSet<HttpSession>();
			act.getApplication().put("uol", uol);
		}

		for (HttpSession sesssion : uol) {
			Users u = (Users) sesssion.getAttribute("user");
			if (u!=null&&u.getUname().equals(user.getUname()))

			{
				log = 1;
				return "login";// 已经登录的情况

			}
		}
		Users u = getUserService().login(user);
		if (u != null) {
			if (u.getUstate().intValue() == 1) {
				log = 2;
				return "login";// 用户被锁定的时候
			}

		} else {
			log = 3;
			return "login";// 用户名密码不正确
		}
		ActionContext.getContext().getSession().put("user", u);
		uol.add(ServletActionContext.getRequest().getSession());
		return "index";// 登录成功的时候

	}

	public String logout() {
		Map mp = ActionContext.getContext().getApplication();
		Set<HttpSession> sessions = (Set<HttpSession>) mp.get("uol");
		Iterator<HttpSession> it = sessions.iterator();
		HttpSession session = null;
		if (it.hasNext()) {
			session = it.next();
			if (ServletActionContext.getRequest().getSession() == session) {

				session.removeAttribute("user");
				mp.remove(session);
				log = 4;// 注销成功
				return "login";
			}
		}
		log = 5;
		return "loginfail";

	}
	public String advice(){
		
		if(getUserService().advice(advice)){
			
			return "adsuc";
			
		}
		return "adfail";
		
	}
	public String mkorder(){
		Map map=ActionContext.getContext().getSession();
		Set<Orderlist> car=(Set<Orderlist>) map.get("car");
		Users user=(Users) map.get("user");
		if(getUserService().mkorder(user,car,order)){
			map.remove("car");
			return "detail";
		}
		return "mkorder";
	}
}
